package com.robotserver.orders.jobs.xxljob;

import com.robotserver.orders.constant.CacheKey;
import com.robotserver.orders.param.TbOrderSyncParam;
import com.robotserver.orders.param.VipOrderSyncParam;
import com.robotserver.orders.service.TbOrderService;
import com.robotserver.orders.service.VipOrderService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class VipOrderSyncXxlJob {
    private static Logger logger = LoggerFactory.getLogger(VipOrderSyncXxlJob.class);

    @Autowired
    private TbOrderService tbOrderService;
    @Autowired
    private VipOrderService vipOrderService;
    /**
     * 生产拉取唯品会订单的tokens
     * 每30秒钟执行一次拉取订单（时间范围是3分钟）
     */
    @XxlJob("pushVipOrdersHander_producer")
    public void pushVipOrdersHander_producer() {
        logger.info("--------VipOrderSyncXxlJob pushVipOrdersHander_producer------start------");

        //默认持续时间为2分钟
        Integer duration = 60000;
        String durationStr = "";
        try {
            //接受参数-拉取当前日期往前的几秒订单
            durationStr = XxlJobHelper.getJobParam();
            if(StringUtils.isNotBlank(durationStr)){
                //从XXL-Job接受参数
                duration = Integer.parseInt(durationStr) * 1000;  //秒换算成毫秒
            }
        }catch (Exception e){
            logger.error("xxl-job para set is error {}:"+durationStr);
            //默认设置一分钟
            duration = 60000;
        }

        Long tmptime = System.currentTimeMillis();
        Long end_time = tmptime ;                       //截至时间为当前时间
        Long begin_time = tmptime - duration;             //起始时间为前2分钟
        VipOrderSyncParam vipOrderSyncParam = new VipOrderSyncParam();
        vipOrderSyncParam.setBegin_time(begin_time);
        vipOrderSyncParam.setEnd_time(end_time);
        vipOrderSyncParam.setDownTimeType(4); //更新时间
        vipOrderSyncParam.setRedisQueueKey(CacheKey.vipPayOrderQueue);
        //调用生产者线程，遍历唯品会联盟，往redis队列中添加vipTokens
        vipOrderService.syncOrder_producer(vipOrderSyncParam);
        logger.info("-------------往redis中推入唯品会联盟------vipOrderService=" + vipOrderService.toString());
    }

    /**
     * 消费唯品会tokens拉取各种订单信息
     * 每30秒钟执行一次拉取订单（时间范围是3分钟）
     */
    @XxlJob("pushVipOrdersHander_consumer")
    public void pushVipOrdersHander_consumer() {
        logger.info("--------VipOrderSyncXxlJob pushVipOrdersHander_consumer------start------");
        //调用消费者线程，遍历redis队列，消费vipTokens
        VipOrderSyncParam vipOrderSyncParam = new VipOrderSyncParam();
        vipOrderSyncParam.setRedisQueueKey(CacheKey.vipPayOrderQueue);
        vipOrderSyncParam.setDownTimeType(4);//更新时间
        vipOrderService.syncOrder_consumer(vipOrderSyncParam);
        logger.info("---------------------------消费唯品会联盟-----------------vipOrderSyncParam=" + vipOrderSyncParam.toString());
    }

}
